<template>
  <div>
    <el-row :gutter="20" type="flex">
      <el-col :span="6">
        <el-input
          placeholder="请输入文章标题"
          v-model="listQueryIn.title"
          clearable
          size="small"
        >
        </el-input>
      </el-col>
      <el-col :span="18">
        <btn-search
          @click.native="refreshList()"
          :disabled="sortable"
        ></btn-search>
        <btn-add
          name="新增文章"
          @click.native="handleAdd()"
          :disabled="sortable"
        ></btn-add>
        <el-button
          type="primary"
          size="small"
          icon="el-icon-download"
          @click="handleImport"
          :disabled="sortable"
          >导入文章</el-button
        >
        <btn-delete
          :disabled="multipleSelection.length == 0 || sortable"
          @click.native="handleBatchDelete()"
        ></btn-delete>
        <el-button
          type="primary"
          size="small"
          icon="el-icon-s-promotion"
          @click="handleBatchPublish"
          plain
          :disabled="multipleSelection.length == 0 || sortable"
          >批量发布</el-button
        >
        <el-button
          type="primary"
          size="small"
          icon="el-icon-s-claim"
          @click="handleBatchSubmitAudit(multipleSelection)"
          plain
          :disabled="multipleSelection.length == 0 || sortable"
          >批量提交审核</el-button
        >
        <el-button
          type="primary"
          size="small"
          icon="el-icon-sort"
          @click="handleSort"
          plain
          v-show="!sortable"
          >排序</el-button
        >
        <el-button
          type="primary"
          size="small"
          @click="handleCancelSort"
          plain
          v-show="sortable"
          >取消排序</el-button
        >
        <el-button
          type="primary"
          size="small"
          @click="handleSaveSort"
          v-show="sortable"
          >保存排序</el-button
        >
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { throttle } from 'lodash'
import sortFun from './sortFun'
import submitAudit from './submitAudit'
export default {
  mixins: [sortFun, submitAudit],
  data() {
    return {
      multipleSelection: [],
      listQueryIn: {
        title: '',
      },
    }
  },
  props: ['curJournal'],
  watch: {
    curJournal: {
      handler() {
        this.multipleSelection = []
      },
    },
  },
  mounted() {
    this.$bus.$on('deleteBatchNewsContents', (v) => {
      this.multipleSelection = v
    })
  },
  beforeDestroy() {
    this.$bus.$off('deleteBatchNewsContents')
  },
  methods: {
    handleInput: throttle(function () {
      this.refreshList()
    }, 2000),
    refreshList() {
      this.$bus.$emit('refreshNewsContentsList', this.listQueryIn)
    },
    handleAdd() {
      this.$bus.$emit('showDialogNewsContentsAdd')
    },
    //重置
    handleReset() {
      this.listQueryIn.title = ''
      this.$store.commit('SET_COLUMN', {})
      this.$bus.$emit('refreshNewsContentsList', this.listQueryIn)
    },
    //批量删除
    handleBatchDelete() {
      if (!this.multipleSelection.length) {
        this.warnMsg('请选择要删除的文章')
        return
      }
      this.deleteTips(this.deleteAll)
    },
    //批量删除
    async deleteAll() {
      let ids = this.multipleSelection.map((v) => v.id)
      await this.$store.dispatch('deleteBatchNewsContents', ids)
      this.$bus.$emit('refreshNewsContentsList')
      this.multipleSelection = []
    },
    handleImport() {
      this.$bus.$emit('showImportSysNewsContents')
    },
    //批量发布
    async handleBatchPublish() {
      if (!this.multipleSelection.length) {
        this.warnMsg('请选择要发布的文章')
        return
      }
      let passList = this.multipleSelection.filter((v) => v.auditStatus == 2)
      if (!passList.length) {
        this.warnMsg('请选择已审核通过的文章')
        return
      }
      let obj = {
        ids: passList.map((v) => v.id),
        status: 1,
      }
      await this.$store.dispatch('changeStatusBatchNewsContents', obj)
      this.$bus.$emit('refreshNewsContentsList')
      this.multipleSelection = []
    },
  },
}
</script>